<?php
/**
 * 供应信息管理
 * 
 * @author  Lee<a605333742@gmail.com>
 * @time    2016-07-26 
 */
namespace Admin\Controller;

class SupplyController extends CommonController{
    /*
     * 构造函数
     * 
     * @return  #
     */
    public function _initialize() {
        parent::_initialize();
        $this->db       =new \Model\SupplyModel();
    }    
    
    /*
     * 已审核的供应信息
     * 
     * @return #
     */
    public function index(){
        $map    =$this->_search("Supply");
        if(session('type')<2){
            $map['publisherId'] =session("authId");
        }
        $map['state']   =2;
        $sortBy         ="top";
        $sort           =desc;
        $infoList       =$this->_list($this->db,$map,$sortBy,$sort);
        $this->assign("infoList",$infoList);
        $this->assign("act",1);
        $this->display();
    }    
    
    /*
     * 待审核的供应信息
     * 
     * @return #
     */
    public function auditSupplyInfo(){
        $map    =$this->_search("Supply");
        if(session('type')<2){
            $map['publisherId'] =session("authId");
        }
        $map['state']   =1;
        $sortBy         ="top";
        $sort           =desc;
        $infoList       =$this->_list($this->db,$map,$sortBy,$sort);
        $this->assign("infoList",$infoList);
        $this->assign("act",2);
        $this->display('index');
    }
    
    /*
     * 显示编辑供应信息（包括新增和修改）
     * 
     * @return #
     */
    function show(){
        $region =D("Region")->select();
        $this->assign("region",$region);
        if(I("request.id","","int")){
            $buyInfo=$this->db->find(I("request.id","","int"));
            $this->assign("buyInfo",$buyInfo);
        }
        $this->display();
    }
    
    /*
     * 审核、批量待审核信息
     * 
     * @return bool
     */
    public function alledit(){       
        $map['id']      =I("request.id");
        $map['state']   =2;
        if($this->db->edits($map)){            
            $this->success("审核成功",U("index"));
        }else{
            $this->error("审核失败",U("index"));
        }
    }
    
    /*
     * 待审核，批量待审核
     * 
     * @return #
     */
    public function audit(){
        $map['id']      =I("request.id");
        $map['state']   =1;
        if($this->db->edits($map)){            
            $this->success("待审核成功",U("index"));
        }else{
            $this->error("待审核失败",U("index"));
        }
    }
    
    /*
     * 删除，批量删除求购信息
     * 
     * @return #
     */
    public function del(){
        $map['id']      =I("request.id");
        $map['state']   =3;
        if($this->db->edits($map)){            
            $this->success("删除成功",U("index"));
        }else{
            $this->error("删除失败",U("index"));
        }
    }
    
    /*
     * 编辑求购信息，包括新增和修改
     * 
     * @return #
     */
    public function edit(){
        if(I("request.id")){
            $map['id']          =I("request.id",'','int');
        }
        if(I("request.title")){
            $map['title']       =I("request.title",'','trim');
        }else{
            $this->error("供应主题不能为空");
        }
        if(I("request.content")){
            $map['content']     =I("request.content",'',"trim");
        }
        if(I("request.publisherAddr")){
            $map['publisherAddr']=I("request.publisherAddr",'',"trim");
        }
        if(I("request.top")){
            $map['top_sign']    =1;
        }
        $map['regionId']        =I("request.regionId",'','int');
        if($this->db->buyEdit($map)){
            $this->success("供应信息编辑成功");
        }else{
            $this->error("供应信息编辑失败");
        }
    }
}
